/*
 * VyzivaciDialog.java
 * 
 * Created on 24.11.2007, 1:34:07
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.gui.dialog;

import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import layout.TableLayout;
import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;

/**
 *
 * @author mara
 * 
 * dialog se objevi pokud tohoto hrace nekdo vyzval
 */
public class VyzivaciDialog extends JDialog {
    
    public static final int X1 = 400;
    public static final int Y1 = 250;
    public static final int WIDTH = 350;
    public static final int HEIGHT = 150;
    
    public static final int CONTEXT_VYZVA_K_HRE = 0;
    public static final int CONTEXT_VYZVA_K_REMIZE = 1;
    public static final int CONTEXT_VYZVA_K_NACTENI_HRY = 2;
    
    private JLabel vyzvaLabel;
    private JButton acceptButton;
    private JButton declineButton;
    
    // indikuje zda byl dialog zavren pres krizek
    private boolean closeFlag;
    
    // Jmeno vyzivatele
    private String vyzivatelName;
    
    // klic hry
    private String gameKey;
    
    // Aktualni instance Vyzivaciho dialogu. Vyzivaci dialog muze byt jen jeden
    private static VyzivaciDialog currentInstance;
    
    // Pomocny flag kvuli spatne funkcnosti pri null hracich
  //  private static boolean pomPlayerNullFlag = false;
    
    
    // kontext dialogu: 0 - pri vyzve ke hre, 1 - pri navrhu remizy, 2 - pri navrhu nacteni hry
    private int context;

    public int getContext() {
        return context;
    }

    public void setContext(int context) {
        this.context = context;
    }        

//    public static  boolean isPomPlayerNullFlag() {
//        return pomPlayerNullFlag;
//    }
//
//    public static void setPomPlayerNullFlag(boolean pomPlayerNullFlagg) {
//       pomPlayerNullFlag = pomPlayerNullFlagg;
//    }        

    public String getVyzivatelName() {
        return vyzivatelName;
    }

    public void setVyzivatelName(String vyzivatelName) {
        this.vyzivatelName = vyzivatelName;
    }        
    
    double size[][] =
        {{30, 120, 50, 120, 30},
        {20, 30, 5, 20, 45}};
    
 // Dialog se z externiho prostredi nevytvari pres konstruktor ale pres metodu getInstance
    private VyzivaciDialog() {}

    private VyzivaciDialog(Frame frame, String title, boolean modal) {
        super(frame, title, modal);
    }
    
    // nastava v pripade, ze je dialog zobrazen v kontextu vyzvy
    public static VyzivaciDialog getInstance(String vyzivatelName, String gameKey) {                                  
        JFrame frame = new JFrame(PropertiesManager.getInstance().getMessageForKey(Text.VYZIVACIDIALOG));
        frame.setBounds(X1, Y1, WIDTH, HEIGHT);          
        VyzivaciDialog dialog = new VyzivaciDialog(frame, PropertiesManager.getInstance().getMessageForKey(Text.VYZIVACIDIALOG), true);        
        VyzivaciDialog.currentInstance = dialog;
        dialog.setBounds(X1, Y1, WIDTH, HEIGHT);         
        dialog.setVyzivatelName(vyzivatelName);
        dialog.setGameKey(gameKey);
        dialog.setContext(CONTEXT_VYZVA_K_HRE);
        dialog.initContents();
        dialog.setVisible(true);           
        return dialog;        
    }
    
    // vraci instanci na zaklade kontextu. Pro jine kontexty nez 0 muze byt vyzivatelName null
    public static VyzivaciDialog getInstance(String vyzivatelName, String gameKey, int context) {
        if (context == CONTEXT_VYZVA_K_HRE) return getInstance(vyzivatelName, gameKey);
        if (context == CONTEXT_VYZVA_K_REMIZE) return getInstanceRemiza();
        if (context == CONTEXT_VYZVA_K_NACTENI_HRY) return getInstanceNacteni();  
        return null;
    }
    
    // nastava v pripade, ze je dialog zobrazen v kontextu navrhu remizy
    public static VyzivaciDialog getInstanceRemiza() {
        JFrame frame = new JFrame(PropertiesManager.getInstance().getMessageForKey(Text.REMIZA_DIALOG_NADPIS));
        frame.setBounds(X1, Y1, WIDTH, HEIGHT);          
        VyzivaciDialog dialog = new VyzivaciDialog(frame, PropertiesManager.getInstance().getMessageForKey(Text.REMIZA_DIALOG_NADPIS), true);        
        VyzivaciDialog.currentInstance = dialog;
        dialog.setBounds(X1, Y1, WIDTH, HEIGHT);    
        dialog.setContext(CONTEXT_VYZVA_K_REMIZE);
        dialog.initContents();
        dialog.setVisible(true);           
        return dialog;    
    }
    
    // nastava v pripade, ze je dialog zobrazen v kontextu nacteni hry, kterou nacita protivnik
    // a po souhlasu nam ji posle.
    public static VyzivaciDialog getInstanceNacteni() {
        JFrame frame = new JFrame(PropertiesManager.getInstance().getMessageForKey(Text.LOAD_DIALOG_NADPIS));
        frame.setBounds(X1, Y1, WIDTH, HEIGHT);          
        VyzivaciDialog dialog = new VyzivaciDialog(frame, PropertiesManager.getInstance().getMessageForKey(Text.LOAD_DIALOG_NADPIS), true);        
        VyzivaciDialog.currentInstance = dialog;
        dialog.setBounds(X1, Y1, WIDTH, HEIGHT);    
        dialog.setContext(CONTEXT_VYZVA_K_NACTENI_HRY);
        dialog.initContents();
        dialog.setVisible(true);           
        return dialog;    
    }
    
    public static VyzivaciDialog getCurrentInstance() {
        return currentInstance;
    }
    
    public void initContents() {
       Container panel = getContentPane();
       panel.setLayout(new TableLayout(size));
       closeFlag = true;
        // Pridavani komponent do hlavniho panelu
       vyzvaLabel = new JLabel(); 
       if (getContext() == CONTEXT_VYZVA_K_HRE) vyzvaLabel.setText(Text.getVyzvaMessage(vyzivatelName, PanelHolderUtils.getDisplayNameByKey(gameKey)));
       if (getContext() == CONTEXT_VYZVA_K_REMIZE) vyzvaLabel.setText(PropertiesManager.getInstance().getMessageForKey(Text.REMIZA_DIALOG_ZPRAVA));
       if (getContext() == CONTEXT_VYZVA_K_NACTENI_HRY) vyzvaLabel.setText(PropertiesManager.getInstance().getMessageForKey(Text.LOAD_DIALOG_ZPRAVA));
       vyzvaLabel.setHorizontalAlignment(JLabel.CENTER);
       acceptButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.ACCEPT));
       declineButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.DECLINE));
       
       acceptButton.addActionListener(new AcceptButtonListener());
       declineButton.addActionListener(new DeclineButtonListener());
       panel.add(vyzvaLabel, "1,1,3,1");
       panel.add(acceptButton, "1,3");
       panel.add(declineButton, "3,3");
       addWindowListener(new DialogCloseListener());   
       setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    
    public JButton getDeclineButton() {
        return declineButton;
    }
    
    // pri dispose u dialogu nastavime currentInstance na null.    
    public void dispose() {
        super.dispose();
        currentInstance = null;
    }
    
    private class AcceptButtonListener implements ActionListener {
        
        // nastava pri stisku tlacitka 'prijmout'
        public void actionPerformed(ActionEvent ae) {              
            closeFlag = false;
            VyzivaciDialog dialog = VyzivaciDialog.getCurrentInstance();
//            pomPlayerNullFlag = true;
            dialog.dispose();  
            if (getContext() == CONTEXT_VYZVA_K_HRE) NastaveniShowPanel.getConnectThread().acceptVyzva();               
            if (getContext() == CONTEXT_VYZVA_K_REMIZE) NastaveniShowPanel.getConnectThread().acceptRemiza();  
            if (getContext() == CONTEXT_VYZVA_K_NACTENI_HRY) NastaveniShowPanel.getConnectThread().acceptLoadGame();            
        }
    }
    
    private class DeclineButtonListener implements ActionListener {
        
        // nastava pri stisku tlacitka 'odmitnout'
        public void actionPerformed(ActionEvent ae) {
            if (getContext() == CONTEXT_VYZVA_K_HRE) NastaveniShowPanel.getConnectThread().declineVyzva();
            if (getContext() == CONTEXT_VYZVA_K_REMIZE) NastaveniShowPanel.getConnectThread().declineRemiza();
            if (getContext() == CONTEXT_VYZVA_K_NACTENI_HRY) NastaveniShowPanel.getConnectThread().declineLoadGame();            
//            pomPlayerNullFlag = true;
            closeFlag = false;
            VyzivaciDialog dialog = VyzivaciDialog.getCurrentInstance();
            dialog.dispose();                            
        }
    }
    
    class DialogCloseListener extends WindowAdapter {
          
        // zamitame vyzvu v pripade, ze uzivatel zavrel dialog
         public void windowClosing(WindowEvent e) { 
            if (closeFlag) {
                       if (getContext() == CONTEXT_VYZVA_K_HRE) NastaveniShowPanel.getConnectThread().declineVyzva();                        
                       if (getContext() == CONTEXT_VYZVA_K_REMIZE) NastaveniShowPanel.getConnectThread().declineRemiza();
                       if (getContext() == CONTEXT_VYZVA_K_NACTENI_HRY) NastaveniShowPanel.getConnectThread().declineLoadGame();
//                       pomPlayerNullFlag = true;
            }                       
         }
                                              }

	public String getGameKey() {
		return gameKey;
	}

	public void setGameKey(String gameKey) {
		this.gameKey = gameKey;
	}

}
